	mui.init();
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    var sale_country="中国";
    var money=0;
	var bankData=[];
	
	refresh_bank();

	mui.plusReady(function() {
		var width=document.documentElement.clientWidth;
    	var height=document.documentElement.clientHeight;
    	document.getElementById('account_pwd').style.width=width+'px';
    	document.getElementById('account_pwd').style.height=height+'px';
 	
		//点中国
		document.getElementById('country2').addEventListener('tap', function() {
			  //打开区域选择
			  mui.openWindow({
			    id:'area_select.html',
			    url:'/pages/transaction/area_select.html',
			    extras:{
			      	 selected_country: document.getElementById('country2').innerHTML, //自定义扩展参数，可以用来处理页面间传值
			         type: 2
			    },
			    show: {
					aniShow: 'pop-in'
				},
				waiting: {
					autoShow: false
				}
			  });
		}, false);
		
		//点绑定银行卡
		var cityPicker = new mui.PopPicker({
			layer: 2
		});
		//点绑定银行卡
		document.getElementById('select_card').addEventListener('tap', function() {
					/*
					二级联动示例
					var cityData = [{
						type: '1',
						value: '1',
						text: '银行卡支付',
						children: [{
								value: "建设银行",
								id:1,
								text: "138000000000"
							}, {
								value: "工商银行",
								id:2,
								text: "25000000000"
							}, {
								value: "农业银行",
								id:3,
								text: "21000000000"
							}]
						},{
							type: '2',
							value: '2',
							text: '支付宝',
							children: [{
									value: "支付宝",
									id:4,
									text: "270000000000"
								}]
							},
							{
							type: '3',
							value: '3',
							text: '微信',
							children: [{
									value: "微信",
									id:5,
									text: "390000000"
								}]
							}];
	
						cityPicker.setData(cityData);
					
						cityPicker.show(function(items) {
							alert( "你选择的城市是:" + items[0].text + " " + items[1].id);
							//返回 false 可以阻止选择框的关闭
							//return false;
						});
						*/
						if(bankData.length==0){
							mui.ajax({
								url: apiurl + "Trade/get_all_bank/",
								data: {'_token':userinfo.token},
								async: true,
								dataType: 'json',
								crossDomain: true, //强制使用5+跨域
								type: 'get',
								timeout: 10000,
								success: function(res) {
									//console.log(JSON.stringify(res.data));
									if(res.status ==-3){
										mui.toast(res.msg);
										app.out();
										return false;
									}
									
									var i=0;
									for(z in res.data){
										bankData[i]=res.data[z];
										i++;
									}
									
									console.log(JSON.stringify(bankData));
									set_banks();
								},
								error: function(xhr, type, errorThrown) {
									//console.log(JSON.stringify(xhr));
									//console.log(JSON.stringify(type));
									//console.log(JSON.stringify(errorThrown));
									mui.toast('操作失败！');
								}
							});
					}
					else{
						set_banks();
					}
						

		}, false);
		
		function set_banks(){
			cityPicker.setData(bankData);
			cityPicker.show(function(items) {
				//alert( "你选择的城市是:" + items[0].text + " " + items[1].id);
				document.getElementById('true_name').innerHTML=items[1].account;
				document.getElementById('bank_name').innerHTML=items[1].value+' 收款';
				document.getElementById('bank_no').innerHTML=items[1].text;
				document.getElementById('bank_addr').value='';
				document.getElementById('bank_id').value=items[1].id;
				
			});
		}
		
		/*
		document.getElementById('select_card').addEventListener('tap', function() {
			  //打开选择银行卡页面
			  mui.openWindow({
			    id:'select_card.html',
			    url:'/pages/setting/select_card.html',
			    extras:{
			        type: 2
			    },
			    show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			  });
		}, false);*/
		
		document.getElementById('buy_order').addEventListener('tap', function() {
			//打开买入页
			mui.openWindow({
				id: 'buy-in.html',
				url: '/pages/buy-in/buy_in.html',
				createNew: true,
				show: {
					aniShow: 'fade-in'
				},
				waiting: {
					autoShow: false
				}
			});
		}, false);
		
		mui("#sale_list").on('tap','.mairu_list.navigation button',function(){
			var targets = this.getAttribute("id");
			if(targets=='create_order'){
				return true;
			}
			mui.openWindow({
			 	id: targets,
			 	url: targets,
			 	show: {
			 		autoShow: false
			 	},
			 	waiting: {
			 		autoShow: true,
			 		title: '正在加载...'
			 	}
			 });
			 
		}, false);
		
		mui("#sale_list").on('tap','.mairu_list.figure button',function(){
			/*
    		if(parseInt(this.innerHTML.trim())!=500){
    			mui.alert('只能挂卖500');
    			return false;
    		}*/
    		if(document.querySelector('#sale_list .mairu_list.figure .mui-btn.selected')){
    			document.querySelector('#sale_list .mairu_list.figure .mui-btn.selected').className='mui-btn';
    		}
    		this.className='mui-btn selected';

    		money=parseInt(this.innerHTML.trim());
    		
    	},false);
    	
		document.getElementById('create').addEventListener('tap', function() {
			if(money==0){
				mui.alert('请选择卖出数额');
				return;
			}
			if(document.querySelector('.transaction-title .bankcard').style.display=='none' || !document.getElementById('bank_no').innerHTML || !document.getElementById('bank_name').innerHTML)
			{
				mui.alert('请添加银行卡信息');
				return;
			}
			
			var content=document.getElementById('content').value.trim();
			if(content.length>200){
				mui.alert('问题反馈最多输入200个字');
				return;
			}
			
			document.getElementById('account_pwd').style.display="block";
			document.getElementById('main_content').style.display="block";
			document.getElementById('keyboard').style.display="block";
			var pwd_array=mui('.mui-input-row.mui-password input[type="password"]');
				mui.each(pwd_array,function(index,item){
				   if(item.value!=''){
				   	   item.value='';
				   }
				}) ;
		});
		
		
		document.getElementById('close_win').addEventListener('tap', function() {
			document.getElementById('account_pwd').style.display="none";
			document.getElementById('main_content').style.display="none";
			document.getElementById('keyboard').style.display="none";
		})
		
		//键盘点击删除
		mui('#keyboard').on('tap', '#numC', function() {
			var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
			var len = pwd_array.length;
			for(i = len - 1; i >= 0; i--) {
				if(pwd_array[i].value != '') {
					pwd_array[i].value = '';
					break;
				}
			}
		});
		
		//点击数字
		mui('#keyboard').on('tap', '.num.data_num', function() {
			var number = this.getAttribute('data-num');
			var pwd_array = mui('.mui-input-row.mui-password input[type="password"]');
			var f = false;
			var pwd = '';
			mui.each(pwd_array, function(index, item) {
				if(item.value == '') {
					item.value = number;
					return false;
				}
			});
		
			mui.each(pwd_array, function(index, item) {
				if(item.value == '') {
					f = true;
					return false;
				}
				pwd += item.value;
			});
		
			if(!f) {
				//mui.alert('正在后台处理...');
				plus.nativeUI.showWaiting("加载中...")
				var Info = {
					user_id: userinfo.user_id,
					trade_pwd: pwd,
					type: 2,
					money: money,
					bank_name: document.getElementById('bank_name').innerHTML.trim(),
					bank_addr: document.getElementById('bank_addr').value.trim(),
					bank_account: document.getElementById('true_name').innerHTML.trim(),
					bank_card: document.getElementById('bank_no').innerHTML.trim(),
					bank_id: document.getElementById('bank_id').value.trim(),
					country: sale_country,
					remark: document.getElementById('content').value.trim(),
					_token:userinfo.token
				}
				document.getElementById('account_pwd').style.display = "none";
				document.getElementById('main_content').style.display = "none";
				document.getElementById('keyboard').style.display = "none";
				mui.ajax({
					url: apiurl + "Trade/do_trade/",
					data: Info,
					async: true,
					dataType: 'json',
					crossDomain: true, //强制使用5+跨域
					type: 'post',
					timeout: 10000,
					success: function(res) {
						console.log(JSON.stringify(res));
						plus.nativeUI.closeWaiting();
						if(res.status ==-3){
							mui.toast(res.msg);
							app.out();
							return false;
						}
						
						// 请求成功
						if(res.status < 1) {
							mui.alert(res.msg);
						} else {
							mui.alert('创建订单成功');
							userinfo.a1_money = res.data.a1_money;
							userinfo.a2_money = res.data.a2_money;
							app.setState(userinfo);
							
							document.querySelector('#sale_list .mairu_list.figure .mui-btn.selected').className='mui-btn';
							//mui.fire(plus.webview.getWebviewById('order.html'), 'refresh_data');
							//mui.fire(plus.webview.getWebviewById('wallet.html'), 'refresh_data');
							mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
						}
					},
					error: function(xhr, type, errorThrown) {
						//console.log(JSON.stringify(xhr));
						//console.log(JSON.stringify(type));
						//console.log(JSON.stringify(errorThrown));
						mui.toast('操作失败！');
					}
				});	
			}
	
		})
			
		
		window.addEventListener('change_state',function(event){
			var selcountry=event.detail.selcountry;
	        document.getElementById('country2').innerHTML=selcountry;
		    sale_country=selcountry;
		});
		
		
		window.addEventListener('refresh_bank',function(event){
		   refresh_bank();
		});
		
		window.addEventListener('seletced_card',function(event){
		    document.getElementById('true_name').innerHTML=event.detail.bank_account;
			document.getElementById('bank_name').innerHTML=event.detail.bank_name;
			document.getElementById('bank_no').innerHTML=event.detail.bank_card;
			document.getElementById('bank_addr').value=event.detail.bank_addr;
		});
		
	});
	
	
	function refresh_bank(){
		mui.ajax({
				url: apiurl + "Trade/get_def_bank/",
				data: {'user_id':userinfo.user_id,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					//console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					
					// 请求成功
					userinfo.bank=res.data;
					app.setState(userinfo);
					if(res.status==0){
						document.querySelector('.transaction-title .unbank').style.display='block';
						document.querySelector('.transaction-title .bankcard').style.display='none';
					}
					else{
						document.getElementById('true_name').innerHTML=res.data.bank_account;
						document.getElementById('bank_name').innerHTML=res.data.bank_name;
						document.getElementById('bank_no').innerHTML=res.data.bank_card;
						document.getElementById('bank_addr').value=res.data.bank_addr;
						document.getElementById('bank_id').value=res.data.id;
						document.querySelector('.transaction-title .unbank').style.display='none';
						document.querySelector('.transaction-title .bankcard').style.display='block';
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
	}
	
	
	function add_card(){
	    	//打开银行卡页
			mui.openWindow({
				id: 'addcard.html',
				url: '/pages/setting/addcard.html',
				extras: {
					bank_id: 0 //自定义扩展参数，可以用来处理页面间传值
				},
				show: {
					autoShow: false
				},
				waiting: {
					autoShow: true,
					title: '正在加载...'
				}
			});
	    }